package com.zhoutian.service.Impl;

import com.zhoutian.dao.SearchRecruitmentInfoDAO;
import com.zhoutian.entity.Message;
import com.zhoutian.entity.MessageRequest;
import com.zhoutian.entity.Page;
import com.zhoutian.service.SearchRecruitmentInfoService;
import com.zhoutian.vo.MessageVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
@Slf4j
public class SearchRecruitmentInfoServiceImpl implements SearchRecruitmentInfoService {

    @Autowired
    private SearchRecruitmentInfoDAO searchRecruitmentInfoDAO;

    @Override
    public List<Message> findAll() {
        return searchRecruitmentInfoDAO.findAll();
    }

    @Override
    public List<Message> findPaginated(Page page) {
        page.setPageStart((page.getPageNum() - 1) * page.getPageSize());
        // 没用了
        page.setPageEnd(page.getPageNum() * page.getPageSize());
        log.info("pageStart: [{}], pageEnd: [{}]", page.getPageStart(), page.getPageEnd());
        return searchRecruitmentInfoDAO.findByPageSizeAndPageNum(page);
    }

    @Override
    public Integer findPageTotal(Page page) {
        int temp = searchRecruitmentInfoDAO.findMessageTotal();
        int pageTotal = temp / page.getPageSize();
        if (temp % page.getPageSize() != 0) {
            pageTotal ++;
        }
        log.info("pageTotal: [{}]", pageTotal);
        return pageTotal;
    }

    @Override
    public MessageVO findByKey(MessageRequest messageRequest) {
        messageRequest.setPageStart((messageRequest.getPageNum() - 1) * messageRequest.getPageSize());
        // 没用
        messageRequest.setPageEnd(messageRequest.getPageNum() * messageRequest.getPageSize());
        List<Message> messageList = searchRecruitmentInfoDAO.findByKeyMessageList(messageRequest);
        log.info("pageStart: [{}], pageEnd: [{}]", messageRequest.getPageStart(), messageRequest.getPageEnd());
        log.info("messageList: [{}]", messageList);
        String key = messageRequest.getKey();
        Integer temp = searchRecruitmentInfoDAO.findByKeyPageTotal(key);
        int pageTotal;
        if (temp != null) {
            pageTotal = temp / messageRequest.getPageSize();
            if (temp % messageRequest.getPageSize() != 0) {
                pageTotal ++;
            }
        } else {
            pageTotal = 0;
        }
        log.info("pageTotal: [{}]", pageTotal);
        MessageVO messageVO = new MessageVO();
        messageVO.setMessages(messageList)
                .setPageTotal(pageTotal);

        return messageVO;
    }
}
